clear
version 8.0
#delimit ;
cd "Z:\interactionmodels\";                       * SET WORKING DIRECTORY;

capture mkdir "correction";
log using correction\correction.log, replace;

*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      correction.do                                   *;
*       Date:           01/11/2007                                      *;
*       Author:         MRG                                             *;
*       Purpose:        We have been able to replicate all of the       *;
*                       results presented in the Electoral Studies      *;
*                       paper. However, when conducting the replication *;
*                       process, we noticed a couple of typos relating  *;
*                       to the corrected data part of our analysis.     *;
*                       As we point out in the paper, our claims do not *;
*                       not depend on whether wer use the MSG data or   *;
*                       our corrected data.  However, this do-file      *;
*                       examines what happens when we correct these     *;
*                       typos.                                          *;
*       Input File:     golder1.dta                                     *;
*       Output File:    correction.log                                  *;
*       Data Output:    none                                            *;
*       Previous file:                                                  *;
*       Machine:                                                        *;
*     ****************************************************************  *;
*     ****************************************************************  *;

set mem 10m;

*     ****************************************************************  *;
*       First, we noted a typo in how we recoded MSG's concentration    *;
*       variable.  We now correct this.                                 *;
*     ****************************************************************  *;

use getdata\golder1.dta;
                    
gen conc1 = concentration;
replace conc1 = 3 if (fragmentation==1 & concentration==0);
replace conc1 = 3 if conc1>3 & conc1!=.;

gen frag_conc1 = fragmentation*conc1;
gen logmag_conc1_nyu = logmag_nyu*conc1;
gen frag_conc1_logmag_nyu = fragmentation*conc1*logmag_nyu;


regress legparties_nyu fragmentation conc1 logmag_nyu 
        frag_conc1 logmag_frag_nyu logmag_conc1_nyu 
        frag_conc1_logmag_nyu proximity_nyu prescandidate_nyu 
        prox_prescandidate_nyu, robust;

*     ****************************************************************  *;
*       These are the results that should be in the last column of      *;
*       Table 1 in the "Fully-Specified Corrected Data" column.         *;
*     ****************************************************************  *; 

*     ****************************************************************  *;
*       Now we need to see how these changes in the results affects the *;
*       the right-hand side figures in the paper. First, let's look at  *;
*       the marginal effect of ethnic fragmentation.                    *;
*     ****************************************************************  *;

*     ****************************************************************  *;
*       Create x-axis for modifying variable (LOGMAG10) = JH            *;
*     ****************************************************************  *;

generate JH=((_n-1)/10);    
replace JH=. if _n>30;

generate str1 txt="*";


*     ****************************************************************  *;
*       Grab elements of the matrix required for calculating            *;
*       conditional coefficients and standard errors.                   *;
*     ****************************************************************  *;

matrix b=e(b);
matrix V=e(V);
scalar b1=b[1,1];
scalar b2=b[1,2];
scalar b3=b[1,3];
scalar b4=b[1,4];
scalar b5=b[1,5];
scalar b6=b[1,6];
scalar b7=b[1,7];



scalar varb1=V[1,1];
scalar varb2=V[2,2];
scalar varb3=V[3,3];
scalar varb4=V[4,4];
scalar varb5=V[5,5];
scalar varb6=V[6,6];
scalar varb7=V[7,7];

scalar covb1b4=V[1,4];
scalar covb1b5=V[1,5];
scalar covb1b7=V[1,7];
scalar covb4b5=V[4,5];
scalar covb4b7=V[4,7];
scalar covb5b7=V[5,7];

set more off;

scalar list b1 b2 b3 b4 b5 b6 b7 varb1 varb2 varb3 varb4 varb5 varb6 varb7 
            covb1b4 covb1b5 covb1b7 covb4b5 covb4b7 covb5b7;

*     ****************************************************************  *;
*         Create full range of conditional coefficients for             *;
*       fragmentation                                                   *;
*     ****************************************************************  *;

gen conb0=b1+b4*0+b5*JH+b7*(0*JH) if _n<30;
gen conb1=b1+b4*1+b5*JH+b7*(1*JH) if _n<30;
gen conb2=b1+b4*2+b5*JH+b7*(2*JH) if _n<30;
gen conb3=b1+b4*3+b5*JH+b7*(3*JH) if _n<30;
gen conb4=b1+b4*4+b5*JH+b7*(4*JH) if _n<30;

set more off;


*     ****************************************************************  *;
*           Create full range of conditional standard errors            *;
*     ****************************************************************  *;

gen conse0=sqrt(varb1
                + varb4*(0^2) + varb5*JH^2 + varb7*JH^2*(0^2)
                + 2*0*covb1b4 + 2*JH*covb1b5 + 2*0*JH*covb1b7+2*0*JH*covb4b5
                + 2*(0^2)*JH*covb4b7 + 2*0*(JH^2)*covb5b7)  if _n<30;
                
gen conse1=sqrt(varb1
                + varb4*(1^2) + varb5*JH^2 + varb7*JH^2*(1^2)
                + 2*1*covb1b4 + 2*JH*covb1b5 + 2*1*JH*covb1b7+2*1*JH*covb4b5
                + 2*(1^2)*JH*covb4b7 + 2*1*(JH^2)*covb5b7)  if _n<30;
                
gen conse2=sqrt(varb1
                + varb4*(2^2) + varb5*JH^2 + varb7*JH^2*(2^2)
                + 2*2*covb1b4 + 2*JH*covb1b5 + 2*2*JH*covb1b7+2*2*JH*covb4b5
                + 2*(2^2)*JH*covb4b7 + 2*2*(JH^2)*covb5b7)  if _n<30;
                
gen conse3=sqrt(varb1
                + varb4*(3^2) + varb5*JH^2 + varb7*JH^2*(3^2)
                + 2*3*covb1b4 + 2*JH*covb1b5 + 2*3*JH*covb1b7+2*3*JH*covb4b5
                + 2*(3^2)*JH*covb4b7 + 2*3*(JH^2)*covb5b7)  if _n<30;
                
gen conse4=sqrt(varb1
                + varb4*(4^2) + varb5*JH^2 + varb7*JH^2*(4^2)
                + 2*4*covb1b4 + 2*JH*covb1b5 + 2*4*JH*covb1b7+2*4*JH*covb4b5
                + 2*(4^2)*JH*covb4b7 + 2*4*(JH^2)*covb5b7)  if _n<30;
                
set more off;

*     ****************************************************************  *;
*                           Create t statistics                         *;
*     ****************************************************************  *;

gen t0=conb0/conse0;
gen t1=conb1/conse1;
gen t2=conb2/conse2;
gen t3=conb3/conse3;
gen t4=conb4/conse4;

*     ****************************************************************  *;
*       Generate a variable equal to conditional betas                  *;
*     ****************************************************************  *;

gen consb0=conb0;
gen consb1=conb1;
gen consb2=conb2;
gen consb3=conb3;
gen consb4=conb4;




*     ****************************************************************  *;
*       Replace consb_ = missing if t score not bigger than cutoff      *;
*     ****************************************************************  *;

replace consb0 = . if abs(t0)<2.01;
replace consb1 = . if abs(t1)<2.01;
replace consb2 = . if abs(t2)<2.01;
replace consb3 = . if abs(t3)<2.01;
replace consb4 = . if abs(t4)<2.01;

set textsize 100;

*     ****************************************************************  *;
*       Graph the effect of fragmentation on legislative parties        *;
*       conditional on logmag                                           *;
*     ****************************************************************  *;

graph twoway   line conb0 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb0 JH ,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb1 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb1 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb2 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb2 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb3 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb3 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)        
        ||  ,   
            ysize(6)
            xsize(8)
            xlabel(0 1 2 3, labsize(2.5)) 
            ylabel(-0.5 0 0.5 1 , labsize(2.5))
            yscale(noline)
            xscale(noline)
            legend(off)
            yline(0, lcolor(black)) yline(-0.5  0.5 1, lcolor(white)) 
            xtitle("")
            ytitle("")
        xsca(titlegap(2)) ysca(titlegap(4))
        scheme(s2mono) 
        graphregion(fcolor(white))
        graphregion(margin(r=28));      
       
*     ****************************************************************  *;
*                               Save graph                              *;
*     ****************************************************************  *;

translate @Graph correction\fragmentation_correction.wmf, replace;

drop JH conb0 conb1 conb2 conb3 conb4 consb0 consb1 consb2 consb3 consb4 conse0 conse1 conse2 conse3 conse4 t0 t1 t2 t3 t4 txt;


*     ****************************************************************  *;
*     ****************************************************************  *;
*     ****************************************************************  *;
*       Now, let's look at the marginal effect of district magnitude    *;
*     ****************************************************************  *;
*     ****************************************************************  *;
*     ****************************************************************  *;

regress legparties_nyu  fragmentation conc1 logmag_nyu 
        frag_conc1 logmag_frag_nyu logmag_conc1_nyu 
        frag_conc1_logmag_nyu proximity_nyu prescandidate_nyu 
        prox_prescandidate_nyu, robust;

*     ****************************************************************  *;
*       Create x-axis for modifying variable (FRAGMENTATION) = JH       *;
*     ****************************************************************  *;

generate JH=((_n-1)/10);    
replace JH=. if _n>100;

generate str1 txt="*";


*     ****************************************************************  *;
*       Grab elements of the matrix required for calculating            *;
*       conditional coefficients and standard errors.                   *;
*     ****************************************************************  *;

matrix b=e(b);
matrix V=e(V);
scalar b1=b[1,1];
scalar b2=b[1,2];
scalar b3=b[1,3];
scalar b4=b[1,4];
scalar b5=b[1,5];
scalar b6=b[1,6];
scalar b7=b[1,7];
scalar b8=b[1,8];
scalar b9=b[1,9];
scalar b10=b[1,10];




scalar varb1=V[1,1];
scalar varb2=V[2,2];
scalar varb3=V[3,3];
scalar varb4=V[4,4];
scalar varb5=V[5,5];
scalar varb6=V[6,6];
scalar varb7=V[7,7];
scalar varb8=V[8,8];
scalar varb9=V[9,9];
scalar varb10=V[10,10];


scalar covb1b4=V[1,4];
scalar covb1b5=V[1,5];
scalar covb1b7=V[1,7];
scalar covb3b5=V[3,5];
scalar covb3b6=V[3,6];
scalar covb3b7=V[3,7];
scalar covb5b6=V[5,6];
scalar covb6b7=V[6,7];
scalar covb4b5=V[4,5];
scalar covb4b7=V[4,7];
scalar covb5b7=V[5,7];
scalar covb8b10=V[5,7];


set more off;

scalar list b1 b2 b3 b4 b5 b6 b7 varb1 varb2 varb3 varb4 varb5 varb6 varb7 
            covb1b4 covb1b5 covb1b7 covb4b5 covb4b7 covb5b7;

*     ****************************************************************  *;
*         Create full range of conditional coefficients for logmag      *;
*     ****************************************************************  *;

gen conb0=b3+b5*JH+b6*0+b7*(0*JH) if _n<100;
gen conb1=b3+b5*JH+b6*1+b7*(1*JH) if _n<100;
gen conb2=b3+b5*JH+b6*2+b7*(2*JH) if _n<100;
gen conb3=b3+b5*JH+b6*3+b7*(3*JH) if _n<100;
gen conb4=b3+b5*JH+b6*4+b7*(4*JH) if _n<100;


set more off;


*     ****************************************************************  *;
*           Create full range of conditional standard errors            *;
*     ****************************************************************  *;

gen conse0=sqrt(varb3
                + varb5*JH^2 + varb6*(0^2) + varb7*(JH^2)*(0^2)
                + 2*JH*covb3b5 + 2*0*covb3b6 + 2*0*JH*covb3b7 + 2*0*JH*covb5b6
                + 2*0*(JH^2)*covb5b7) + 2*(0^2)*JH*covb6b7  if _n<100;
                
gen conse1=sqrt(varb3
                + varb5*JH^2 + varb6*(1^2) + varb7*(JH^2)*(1^2)
                + 2*JH*covb3b5 + 2*1*covb3b6 + 2*1*JH*covb3b7 + 2*1*JH*covb5b6
                + 2*1*(JH^2)*covb5b7) + 2*(1^2)*JH*covb6b7  if _n<100;
                
gen conse2=sqrt(varb3
                + varb5*JH^2 + varb6*(2^2) + varb7*(JH^2)*(2^2)
                + 2*JH*covb3b5 + 2*2*covb3b6 + 2*2*JH*covb3b7 + 2*2*JH*covb5b6
                + 2*2*(JH^2)*covb5b7) + 2*(2^2)*JH*covb6b7  if _n<100;
                
gen conse3=sqrt(varb3
                + varb5*JH^2 + varb6*(3^2) + varb7*(JH^2)*(3^2)
                + 2*JH*covb3b5 + 2*3*covb3b6 + 2*3*JH*covb3b7 + 2*3*JH*covb5b6
                + 2*3*(JH^2)*covb5b7) + 2*(3^2)*JH*covb6b7  if _n<100;
                
gen conse4=sqrt(varb3
                + varb5*JH^2 + varb6*(4^2) + varb7*(JH^2)*(4^2)
                + 2*JH*covb3b5 + 2*4*covb3b6 + 2*4*JH*covb3b7 + 2*4*JH*covb5b6
                + 2*4*(JH^2)*covb5b7) + 2*(4^2)*JH*covb6b7  if _n<100;
                
set more off;

*     ****************************************************************  *;
*                           Create t statistics                         *;
*     ****************************************************************  *;

gen t0=conb0/conse0;
gen t1=conb1/conse1;
gen t2=conb2/conse2;
gen t3=conb3/conse3;
gen t4=conb4/conse4;

*     ****************************************************************  *;
*       Generate a variable equal to conditional betas                  *;
*     ****************************************************************  *;

gen consb0=conb0;
gen consb1=conb1;
gen consb2=conb2;
gen consb3=conb3;
gen consb4=conb4;




*     ****************************************************************  *;
*       Replace consb_ = missing if t score not bigger than cutoff      *;
*     ****************************************************************  *;

replace consb0 = . if abs(t0)<2.01;
replace consb1 = . if abs(t1)<2.01;
replace consb2 = . if abs(t2)<2.01;
replace consb3 = . if abs(t3)<2.01;
replace consb4 = . if abs(t4)<2.01;

set textsize 100;

*     ****************************************************************  *;
*       Graph the effect of district magnitude                          *;
*     ****************************************************************  *;

graph twoway   line conb0 JH , clpattern(solid) clwidth(vthin)
        ||  scatter consb0 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb1 JH , clpattern(solid) clwidth(vthin)
        ||  scatter consb1 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb2 JH , clpattern(solid) clwidth(vthin)
        ||  scatter consb2 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb3 JH , clpattern(solid) clwidth(vthin)
        ||  scatter consb3 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)        
        ||  ,   
            ysize(6)
            xsize(8)
            xlabel(0 1 2 3 4 5 6 7 8 9 10, labsize(2.5)) 
            ylabel(-1 0 1 2, labsize(2.5))
            yscale(noline)
            xscale(noline)
            yline(0, lcolor(black)) yline(-4 -3 -2 -1 1 2 3 4 5 6, lcolor(white)) 
            xtitle("")
            ytitle("")
            xsca(titlegap(2))
            ysca(titlegap(4.5))
            text(2.9 2.7 "* indicates significance at the 95% level", justification(left) size(2.5))
            legend(off)
            scheme(s2mono) graphregion(fcolor(white))
            graphregion(margin(r=28));
            
            
*     ****************************************************************  *;
*                               Save picture                            *;
*     ****************************************************************  *;

translate @Graph correction\magnitude_correction.wmf, replace;

drop JH conb0 conb1 conb2 conb3 conb4 consb0 consb1 consb2 consb3 consb4 
conse0 conse1 conse2 conse3 conse4 t0 t1 t2 t3 t4 txt;
           
log close;
exit;







